<?php
require('common_view_functions.php');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Blog 用户后台管理 - 友情链接</title>
<link rel="shortcut icon" href="/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/mgmt.css" />
<link rel="stylesheet" type="text/css" href="/css/3rd/jqModal.css" />
</head>
<body id="bodySettings">
<div id="wrapper">
	<!-- header -->
	<?php require('common_header.php'); ?>
	<!-- /header -->
	<div id="body">
		<div id="side">
			<ul id="settings_cates">
				<li><a href="<?php echo('/' . $blogger_uri . '/mgmt/settings'); ?>">基本设置 &raquo;</a></li>
				<li><a href="<?php echo('/' . $blogger_uri . '/mgmt/categories'); ?>">网志分类与 Tag &raquo;</a></li>
				<li><a href="<?php echo('/' . $blogger_uri . '/mgmt/links'); ?>" class="curr">友情链结 &raquo;</a></li>
			</ul>
		</div>
		<div id="main">
			<div id="linksDiv">
				<h3 class="sectionHeader">新增友情链接</h3>
				<form id="frmNewLink">
					<label for="txtNewLinkTitle">名称：</label>
					<input id="txtNewLinkTitle" class="textBox" maxlength="64" size="16" style="width:24em;" />
					<label for="txtNewLinkUrl">网址：（须包含 http:// 前缀）</label>
					<input id="txtNewLinkUrl" class="textBox" maxlength="256" size="16" style="width:24em;" />
					<label for="txtNewLinkDesc">简介：</label>
					<input id="txtNewLinkDesc" class="textBox" maxlength="256" size="16" style="width:24em;" />
					<input type="button" id="btnAddNewLink" class="button" value="提交" />
				</form>
				<h3 class="sectionHeader">现有链接列表</h3>
				<dl id="linksList">Loading...</dl>
			</div>
		</div>
		<div class="z"></div>
	</div>
	<!-- footer -->
	<?php require('common_footer.php'); ?>
	<!-- /footer -->
</div>

<!-- JS -->
<script type="text/javascript" src="/js/3rd/jquery.pack.js"></script>
<script type="text/javascript" src="/js/3rd/jqModal.js"></script>
<script type="text/javascript" src="/js/3rd/json2.js"></script>
<script type="text/javascript">
<!--
var g_userSiteRoot = "<?php echo('/' . $blogger_uri); ?>";

var modFieldsCreated = false;
var modLinkData = {
	"id": 0,
	"title": "",
	"url": "",
	"desc": ""
};
var delLinkData = {
	"id": 0
};

function addLinkCallback(rdata) {
	$("#ajaxLoading").hide();
	
	$("#txtNewLinkTitle").val("");
	$("#txtNewLinkUrl").val("");
	$("#txtNewLinkDesc").val("");
	
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	loadLinkList();
}

function modLinkCallback(rdata) {
	$("#ajaxLoading").hide();
	rdata = rdata.split("_");
	if(rdata[0] != "SUCC") {
		alert("Operation failed!");
		return;
	}
	
	loadLinkList();
}

function delLinkCallback(rdata) {
	$("#ajaxLoading").hide();
	var lid = delLinkData.id;
	$("#dtl_" + lid).fadeOut();
	$("#ddl_" + lid).fadeOut();
}

function loadLinkListCallback(rdata) {
	$("#linksList dt .linkEditPad a").click(function() {
		var ac = $(this).attr("rel");
		ac = ac.split("_");
		var action = ac[0];
		var lid = ac[1];
		if(action == "e") {
			// show edit-box, spagehtti :(
			if(!modFieldsCreated) {
				modLinkData.id = lid;
				modLinkData.title = $("#dtl_" + lid + " span").text();
				modLinkData.url = $("#dtl_" + lid + " a").attr("rel");
				modLinkData.desc = $("#ddl_" + lid).text();
				var inpModTitle = "<input type=\"text\" id=\"txtModTitle\" class=\"textBox\" maxlength=\"64\" style=\"width:16em;\" value=\"" + modLinkData.title + "\" />";
				var inpModUrl = "<input type=\"text\" id=\"txtModUrl\" class=\"textBox\" maxlength=\"256\" style=\"width:16em;\" value=\"" + modLinkData.url + "\" /><div style='padding:0.25em 0 0 0;'></div>";
				var inpModDesc = "<input type=\"text\" id=\"txtModDesc\" class=\"textBox\" maxlength=\"256\" style=\"width:16em;\" value=\"" + modLinkData.desc + "\" />";
				var inpModButtons = "<input type=\"button\" id=\"btnModLink\" class=\"button\" value=\"确定\" /> <input type=\"button\" id=\"btnModLinkCancel\" class=\"button\" value=\"取消\" />"
				$("#dtl_" + lid + " .wrpr").html(inpModTitle);
				$("#ddl_" + lid).html(inpModUrl + inpModDesc + " " + inpModButtons);
				modFieldsCreated = true;
				$("#dtl_" + lid + " .linkEditPad").hide();
				
				$("#btnModLinkCancel").click(function() {
					$("#dtl_" + lid + " .wrpr").html("<a href=\"" + modLinkData.url + "\" target=\"_blank\"><span></span></a>");
					$("#dtl_" + lid + " span").text(modLinkData.title);
					$("#ddl_" + lid).text(modLinkData.desc);
					modFieldsCreated = false;
					$("#dtl_" + lid + " .linkEditPad").show();
				});
				$("#btnModLink").click(function() {
					modLinkData.id = lid;
					modLinkData.title = $("#txtModTitle").val();
					modLinkData.url = $("#txtModUrl").val();
					modLinkData.desc = $("#txtModDesc").val();
					// do cate-update
					$("#ajaxLoading").show();
					$.post(g_userSiteRoot + "/mgmt/links", {
						"hidIsPostBack": "ja",
						"operation": "update",
						"posted_data": JSON.stringify(modLinkData)
					}, modLinkCallback);					
				});
			}
		} else if(action == "d") {
			// delete cate
			if(confirm("确定要删除该链接？")) {
				// do delete
				delLinkData.id = lid;
				$("#ajaxLoading").show();
				$.post(g_userSiteRoot + "/mgmt/links", {
					"hidIsPostBack": "ja",
					"operation": "delete",
					"posted_data": JSON.stringify(delLinkData)
				}, delLinkCallback);
			}
		}
	});
	
	modFieldsCreated = false;
	$("#ajaxLoading").hide();
}

function loadLinkList() {
	$("#ajaxLoading").show();
	$("#linksList").load(g_userSiteRoot + "/mgmt/links_list", {
		"R": Math.random()
	}, loadLinkListCallback);
}

$(document).ready(function() {
	$("#btnAddNewLink").click(function() {
		var newLinkTitle = $("#txtNewLinkTitle").val();
		var newLinkUrl = $("#txtNewLinkUrl").val();
		if(newLinkTitle == "" || newLinkUrl == "") {
			alert("链接名或网址不能为空！");
			return;
		}
		var newLinkDesc = $("#txtNewLinkDesc").val();
		
		var newLinkData = {
			"title": newLinkTitle,
			"url": newLinkUrl,
			"desc": newLinkDesc
		};
		// do add new
		$("#ajaxLoading").show();
		$.post(g_userSiteRoot + "/mgmt/links", {
			"hidIsPostBack": "ja",
			"operation": "create",
			"posted_data": JSON.stringify(newLinkData)
		}, addLinkCallback);
	});
	
	loadLinkList();
});
// -->
</script>
<!-- /JS -->

<div id="ajaxLoading"><img alt="Loading..." src="<?php echo(base_url() . 'img/ajax_loading.gif'); ?>" border="0" width="24" height="24" /></div>
</body>
</html>